package com.example.takeaway.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.takeaway.entity.Goods;
import com.example.takeaway.serivce.IGoodsService;
import com.example.takeaway.utils.PageResult;
import com.example.takeaway.utils.ServerResponse;
import org.bouncycastle.crypto.digests.GOST3411Digest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * goodsController
 * 
 * @author ruoyi
 * @date 2025-05-15
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询goods列表
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public ServerResponse list(Goods goods, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
//        startPage();
        PageResult pageResult = goodsService.selectGoodsList(goods,pageNum,pageSize);
       return  ServerResponse.createBysuccess(200,pageResult,"查询成功");
    }


    @GetMapping("/goodscategory")
    public ServerResponse category( Long merchantid){
        List<Goods> list = goodsService.selectGoodscategory(merchantid);
        return  ServerResponse.createBysuccess(200,list,"查询成功");
    }
    /**
     * 导出goods列表
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:export')")
//    @Log(title = "goods", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Goods goods)
//    {
//        List<Goods> list = goodsService.selectGoodsList(goods);
//        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
//        util.exportExcel(response, list, "goods数据");
//    }

    /**
     * 获取goods详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{gid}")
    public ServerResponse getInfo(@PathVariable("gid") Long gid)
    {
        Goods goods = goodsService.selectGoodsByGid(gid);
        return ServerResponse.createBysuccess(200,goods,"查询成功");
    }

    /**
     * 新增goods
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:add')")
//    @Log(title = "goods", businessType = BusinessType.INSERT)
    @PostMapping
    public ServerResponse add(@RequestBody Goods goods)
    {

        int i = goodsService.insertGoods(goods);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }

    /**
     * 修改goods
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
//    @Log(title = "goods", businessType = BusinessType.UPDATE)
    @PutMapping
    public ServerResponse edit(@RequestBody Goods goods)
    {
        int i = goodsService.updateGoods(goods);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }

    /**
     * 删除goods
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
//    @Log(title = "goods", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gids}")
    public ServerResponse remove(@PathVariable Long[] gids)
    {
        int i = goodsService.deleteGoodsByGids(gids);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }
}
